package com.ruoyi.project.cms.content.service;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.project.cms.content.domain.Content;
import com.ruoyi.project.cms.content.mapper.ContentMapper;
import com.ruoyi.project.cms.type.mapper.CmsTypeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ContentServiceImpl implements ContentService {
    //声明mapper层属性
    @Autowired
    private ContentMapper contentMapper;
    @Autowired
    private CmsTypeMapper cmsTypeMapper;
    //删除公告类型
    @Override
    public int deleteTypeByIds(String ids) {
        Long[] typeIds = Convert.toLongArray(ids);

       /* for (Long typeId : typeIds)
        {
            //判断要删除的公告类型是否已经存在对应的公告信息
            *//*Post post = selectPostById(postId);
            if (countUserPostById(postId) > 0)
            {
                throw new BusinessException(String.format("%1$s已分配,不能删除", post.getPostName()));
            }*//*
        }*/

        return contentMapper.deleteTypeByIds(typeIds);
    }


    //保存修改公告类别id
    @Override
    public Integer updateCmsType(Content content) {
        content.setTypeName(cmsTypeMapper.getCmsTypeNameMapper(content.getTypeId()));
        return contentMapper.updateCmsTypeMapper(content);
    }

    //获取要修改的公告信息
    @Override
    public Content selectTypeById(Long prId) {
        return contentMapper.selectTypeByIdMapper(prId);
    }

    //公告类型的新增
    @Override
    public int insertCmsType(Content content) {

        return contentMapper.insertCmsType(content);
    }
    //校验类型名称
    @Override
    public String checkTypeNameUnique(Content content) {
        String prContent = StringUtils.isNull(content.getPrContent()) ? "" : content.getPrContent();
        Content info = contentMapper.checkTypeNameUnique(content.getPrId());
        if (StringUtils.isNotNull(info) && info.getPrContent().equals(prContent)) {
            return UserConstants.TYPE_NAME_NOT_UNIQUE;
        }
        return UserConstants.TYPE_NAME_UNIQUE;
    }

    //加载公告类型信息
    @Override
    public List<Content> selectCmsTypeList(Content content) {

        return contentMapper.selectCmsTypeListMapper(content);
    }
}


